﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Wilke.Interactive.Drone.PlayGround
{
    /// <summary>
    /// Interaction logic for Slider.xaml
    /// </summary>
    public partial class Slider : UserControl
    {
        private int maxOffset = 0;
        private int baseOffset = 0;
 
        public Single SliderValue
        {
            set
            {
                Matrix matrix;
                MatrixTransform thumbTransform = thumb.RenderTransform as MatrixTransform;

                if (thumbTransform != null)
                {
                    Single sliderValue = value;
                    if (Math.Abs(sliderValue) > 1)
                    {
                        sliderValue = 1 * Math.Sign(sliderValue);
                    }
                    matrix = thumbTransform.Matrix;
                    matrix.OffsetY = baseOffset - (baseOffset * sliderValue);
                    thumbTransform.Matrix = matrix;
                }
            }
        }

        public event EventHandler<SliderValueChangedEventArgs> SliderValueChanged;

        public Slider()
        {
            InitializeComponent();
        }

        private void elSlider_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            e.ManipulationContainer = this;
            e.Mode = ManipulationModes.TranslateY;
            e.Handled = true;
        }

        private void elSlider_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            ManipulationDelta manipulationDelta = e.DeltaManipulation;
            Canvas canvas = e.OriginalSource as Canvas;

            Matrix matrix = ((MatrixTransform)canvas.RenderTransform).Matrix;
            matrix.Translate(manipulationDelta.Translation.X, manipulationDelta.Translation.Y);
            if (matrix.OffsetY > 0 && matrix.OffsetY < maxOffset)
            {
                canvas.RenderTransform = new MatrixTransform(matrix);
                UpdateValue(matrix.OffsetY);
            }
            e.Handled = true;
        }

        private void CenterThumb()
        {
            Matrix matrix = ((MatrixTransform)thumb.RenderTransform).Matrix;
            matrix.OffsetY = (this.ActualHeight - thumb.ActualHeight) / 2;
            thumb.RenderTransform = new MatrixTransform(matrix);
            UpdateValue(matrix.OffsetY);
        }

        private void userControl_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            CenterThumb();
            e.Handled = true;
        }

        private void UpdateValue(double offsetY)
        {
            Single sliderValue = (float)((baseOffset - offsetY) / baseOffset);

            if (SliderValueChanged != null)
            {
                SliderValueChanged(this, new SliderValueChangedEventArgs(sliderValue));
            }
        }

        private void userControl_Loaded(object sender, RoutedEventArgs e)
        {
            maxOffset = (int)(this.ActualHeight - thumb.ActualHeight) ;
            baseOffset = maxOffset / 2;
            CenterThumb();
        }
    }
}
